#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;
class Solution
{
public:
    string frequencySort(string s)
    {
        unordered_map<char, int> map;
        int len = s.size();
        int maxF = 0;
        for (int i = 0; i < len; i++)
        {
            maxF = max(maxF, ++map[s[i]]);
        }
        vector<vector<char>> b(maxF);
        unordered_map<char, int>::iterator iter;
        for (iter = map.begin(); iter != map.end(); iter++)
        {
            b[iter->second - 1].push_back(iter->first);
        }
        string ans = "";
        for (int i = maxF - 1; i >= 0; i--)
        {
            vector<char> chars = b[i];
            int le = chars.size();
            for (int j = 0; j < le; j++)
            {
                for (int k = 0; k < i + 1; k++)
                {
                    ans.push_back(chars[j]);
                }
            }
        }
        return ans;
    }
};
int main(int argc, char const *argv[])
{
    Solution s;
    cout << s.frequencySort("Aabb") << endl;
    return 0;
}
